package com.app.controller;


import cn.hutool.core.date.DateTime;
import com.alibaba.excel.EasyExcel;
import com.app.model.OrderModel;
import com.app.service.OrderService;
import com.app.vo.ExcelExportDto;
import com.app.vo.OrderExportVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * 账单导出
 */
@RestController
@Controller
@RequestMapping("/excel")
@Slf4j
public class OrderExportController {
    public static final String OrderExportURL = ".../账单导出表.xls";//福利费
    public static final String APPLICATION_OCTET_STREAM_VALUE = "application/octet-stream";


    @Autowired
    private OrderService orderService;

    /**
     * 账单导出
     *
     * @param response
     */
    @GetMapping("/orderExcel")
    public void enjoyExcel( ExcelExportDto dto, HttpServletResponse response) throws IOException {
        //设置响应头
//        response.setContentType("application/vnd.ms-excel");
        response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
        response.setCharacterEncoding("utf-8");
        //设置下载时的文件名
        String time = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy年MM月dd日HH时mm分ss秒"));
        String fileName = "账单数据" + time + ".xlsx";
        //中文名编码格式设置
        fileName = URLEncoder.encode(fileName, "UTF-8");
        //文件下载方式（附件下载还是当前浏览器下载）
        response.setHeader("Content-disposition", "attachment;filename=" + fileName);
        //构建写入到excel中的数据
        //获取列表
        List<OrderModel> list = orderService.getBusinessOrderList(1, 10000, dto.getStatus(), dto.getBusinessName(), dto.getBusinessId(), dto.getBeginTime(), dto.getEndTime()).getList();
        ArrayList<OrderExportVo> orderExportVos = new ArrayList<>();
        for (OrderModel o : list) {
            OrderExportVo exportVo = new OrderExportVo();
            exportVo.setId(o.getId());
            exportVo.setBusinessName(o.getBusinessName());
            exportVo.setVoucher(o.getVoucher());
            exportVo.setActivityName(o.getActivityName());
            exportVo.setOrderId(o.getOrderId());
            if (o.getStatus()==1){
                exportVo.setStatusName("待使用");
            }else if (o.getStatus()==2){
                exportVo.setStatusName("待评价");
            }else if(o.getStatus()==3){
                exportVo.setStatusName("已完成");
            }else {
                exportVo.setStatusName("已完成");
            }
            LocalDateTime createTime = DateTime.of(o.getCreateTime()).toLocalDateTime();
            exportVo.setCreateTime(createTime);
           orderExportVos.add(exportVo);
        }
//        // 输出文件内容到响应流中
        try (OutputStream outputStream = response.getOutputStream()) {
            EasyExcel.write(outputStream, OrderExportVo.class).sheet("模板").doWrite(orderExportVos);
        } catch (IOException e) {
            // 处理异常
        }
    }
}
